package com.tim.utils.android;

import java.util.ArrayList;

import android.graphics.Path;
import android.graphics.Point;

/**
 * 几何运算相关工具
 *
 */
public class GeometryUtil {
	private static final String TAG = "GeometryUtil";
	/**
	 * 求两线段是否有交点
	 * @param line1Point1
	 * @param line1Point2
	 * @param line2Point1
	 * @param line2Point2
	 * @return
	 */
	public static boolean isCrossing(Point line1Point1,Point line1Point2,Point line2Point1,Point line2Point2){
		boolean result = 
			Math.max(line1Point1.x, line1Point2.x)>=Math.min(line2Point1.x, line2Point2.x)
		&&	Math.max(line1Point1.y, line1Point2.y)>=Math.min(line2Point1.y, line2Point2.y)
		&&	Math.max(line2Point1.x, line2Point2.x)>=Math.min(line1Point1.x, line1Point2.x)
		&&	Math.max(line2Point1.y, line2Point2.y)>=Math.min(line1Point1.y, line1Point2.y)
		&&	mult(line2Point1, line1Point2, line1Point1)*mult(line1Point2, line2Point2, line1Point1)>=0
		&&	mult(line1Point1, line2Point2, line2Point1)*mult(line2Point2, line1Point2, line2Point1)>=0;
//		Log.d(TAG, "line1Point1:"+line1Point1.toString()+";line1Point2:"+line1Point2.toString()+";line2Point1:"+line2Point1.toString()+";line2Point2:"+line2Point2.toString());
		return result;
	}
	
	//叉积
	private static double mult(Point a, Point b, Point c)
	{
		return (a.x-c.x)*(b.y-c.y)-(b.x-c.x)*(a.y-c.y);
	}
	
	public static Path calculatePathBy2Point(Point firstPoint,Point endPoint,int pointCount){
		//直线方程:y=kx+b (ps:k为斜率,注意正负)
//		int k = (firstPoint.y-endPoint.y)/(firstPoint.x - endPoint.x);
//		int b = (firstPoint.y-k*firstPoint.x);
		if(firstPoint==null || endPoint==null || pointCount<=0){
			return null;
		}
		int offsetX = Math.abs(firstPoint.x - endPoint.x);
		int offsetY = Math.abs(firstPoint.y - endPoint.y);
		int a = offsetX/pointCount;
		int b = offsetY/pointCount;
		int resultX = firstPoint.x;
		int resultY = firstPoint.y;

		Path path = new Path();
		path.moveTo(firstPoint.x, firstPoint.y);
		path.addCircle(resultX,resultY, 5, Path.Direction.CCW);
		path.moveTo(endPoint.x, endPoint.y);
		path.addCircle(resultX,resultY, 5, Path.Direction.CCW);
		
		for(int i=0;i<pointCount;i++){
			if(firstPoint.x>endPoint.x && firstPoint.y>endPoint.y){
				resultX -= a;
				resultY -= b;
			}else if(firstPoint.x<endPoint.x && firstPoint.y<endPoint.y){
				resultX += a;
				resultY += b;
			}else if(firstPoint.x>endPoint.x && firstPoint.y<endPoint.y){
				resultX -= a;
				resultY += b;
			}else if(firstPoint.x<endPoint.x && firstPoint.y>endPoint.y){
				resultX += a;
				resultY -= b;
			}else if(firstPoint.x==endPoint.x && firstPoint.y>endPoint.y){
				resultY -= b;
			}else if(firstPoint.x==endPoint.x && firstPoint.y<endPoint.y){
				resultY += b;
			}
			path.moveTo(resultX, resultY);
			path.addCircle(resultX,resultY, 5, Path.Direction.CCW);
		}
		path.close();
		
		return path;
	}
	
	public static Path calculatePathBy2Point(Point firstPoint,ArrayList<Point> endPointList,int pointCount){
		if(firstPoint==null || endPointList==null || pointCount<=0 || endPointList.size()==0){
			return null;
		}
		Path path = new Path();
		for(Point endPoint : endPointList){
			int offsetX = Math.abs(firstPoint.x - endPoint.x);
			int offsetY = Math.abs(firstPoint.y - endPoint.y);
			int a = offsetX/pointCount;
			int b = offsetY/pointCount;
			int resultX = firstPoint.x;
			int resultY = firstPoint.y;
			
			path.moveTo(firstPoint.x, firstPoint.y);
			path.addCircle(resultX,resultY, 5, Path.Direction.CCW);
			path.moveTo(endPoint.x, endPoint.y);
			path.addCircle(resultX,resultY, 5, Path.Direction.CCW);
			
			for(int i=0;i<pointCount;i++){
				if(firstPoint.x>endPoint.x && firstPoint.y>endPoint.y){
					resultX -= a;
					resultY -= b;
				}else if(firstPoint.x<endPoint.x && firstPoint.y<endPoint.y){
					resultX += a;
					resultY += b;
				}else if(firstPoint.x>endPoint.x && firstPoint.y<endPoint.y){
					resultX -= a;
					resultY += b;
				}else if(firstPoint.x<endPoint.x && firstPoint.y>endPoint.y){
					resultX += a;
					resultY -= b;
				}else if(firstPoint.x==endPoint.x && firstPoint.y>endPoint.y){
					resultY -= b;
				}else if(firstPoint.x==endPoint.x && firstPoint.y<endPoint.y){
					resultY += b;
				}
				path.moveTo(resultX, resultY);
				path.addCircle(resultX,resultY, 5, Path.Direction.CCW);
			}
		}
		path.close();
		
		return path;
	}
	
	/**
	 * 计算两点间的距离(根据勾股定理c^2=a^2+b^2)
	 * @param firstPoint
	 * @param endPoint
	 * @return
	 */
//	public static int calculateDistanceBy2Point(Point firstPoint,Point endPoint){
//		int a = Math.abs(firstPoint.x-endPoint.x);
//		int b = Math.abs(firstPoint.y-endPoint.y);
//		if(a==0){
//			return b;
//		}else if(b==0){
//			return a;
//		}else {
//			return (int)Math.sqrt(a*a+b*b);
//		}
//	}
}
